package com.hkts.naturalgas.alarminformation.controller;


import com.hkts.naturalgas.alarminformation.mapper.NaturalgasAlarminMapper;
import com.hkts.naturalgas.utils.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.alibaba.fastjson.JSON;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hkts.naturalgas.alarminformation.service.INaturalgasAlarminService;
import com.hkts.naturalgas.alarminformation.entity.NaturalgasAlarmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yck
 * @since 2020-09-11
 */
@RestController
@RequestMapping("/instrumentdata/naturalgasAlarmin")
public class NaturalgasAlarminController {
    private final Logger logger = LoggerFactory.getLogger(NaturalgasAlarminController.class);

    @Autowired
    public INaturalgasAlarminService iNaturalgasAlarminService;
    //

    @ResponseBody
    @GetMapping("/getList")
    public Result<Object> getList(CustomPage page, String stationId, String grade, String id) {

        Page<Map<String, Object>> joinResult = iNaturalgasAlarminService.getJoinResult(CustomPageUtils.getPage(page), stationId, grade, id);
        List<Map<String, Object>> records = joinResult.getRecords();
        SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < records.size(); i++) {
            Date createTime = (Date) records.get(i).get("createTime");
            Date endeTime = (Date) records.get(i).get("endeTime");
            records.get(i).put("createTime", ff.format(createTime));
            records.get(i).put("endeTime", ff.format(endeTime));
            records.get(i).put("alarminInfo",records.get(i).get("equipmentName").toString()+"（数据异常）");
        }
        joinResult.setRecords(records);
        return ResultUtil.data(CustomPageUtils.getCustomPage(joinResult));
    }

    //报警信息导出
    @ResponseBody
    @GetMapping("/exportAlarminList")
    public void exportAlarminList(HttpServletResponse response,String stationId, String grade, String id) {
        List<Map<String, Object>> joinResult = iNaturalgasAlarminService.getExportData( stationId, grade, id);
        String[] fields= {"站点名称","报警对象","等级","报警开始时间","报警结束时间","报警信息"};
        List<String[]> list=new ArrayList<>();
        SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i=0;i<joinResult.size();i++){
            String[] row=new String[6];
            row[0]=joinResult.get(i).get("gateName").toString();
            row[1]=joinResult.get(i).get("equipmentName").toString();
            row[2]= TestDataUtils.getGardeName(joinResult.get(i).get("grade").toString());
            row[3]=ff.format((Date) joinResult.get(i).get("createTime"));
            row[4]=ff.format((Date) joinResult.get(i).get("endeTime"));
            row[5]=joinResult.get(i).get("equipmentName")+"(数据异常)";
            list.add(row);
        }
        HSSFWorkbook book = ExcelUtil.drawExcel("报警信息", fields, list);
        ExcelUtil.downExcel(book,response,"报警信息");
    }

    /**
     * 分页查询数据
     *
     * @param page              分页信息
     * @param naturalgasAlarmin 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getNaturalgasAlarminPageList")
    public Result<Object> getHontUserList(Page<NaturalgasAlarmin> page, NaturalgasAlarmin naturalgasAlarmin) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iNaturalgasAlarminService.selectPage(page, naturalgasAlarmin)));
    }

    /**
     * 保存和修改公用的
     *
     * @param naturalgasAlarmin 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/naturalgasAlarminSave")
    public Result<Object> naturalgasAlarminSave(NaturalgasAlarmin naturalgasAlarmin) {
        iNaturalgasAlarminService.saveOrUpdate(naturalgasAlarmin);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/naturalgasAlarminGetById")
    public Result<Object> naturalgasAlarminGetById(String id) {
        NaturalgasAlarmin byId = iNaturalgasAlarminService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/naturalgasAlarminDelete")
    public Result<Object> hontUserDelete(String id) {
        iNaturalgasAlarminService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/naturalgasAlarminBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iNaturalgasAlarminService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }

}
